package com.miraksoft.statewatcher.helper;

import java.util.List;

import android.app.Activity;
import android.os.SystemClock;
import android.widget.Chronometer;

import com.miraksoft.statewatcher.bean.WatchedObjectRuntimeContainer;
import com.miraksoft.statewatcher.bean.WatchedObjectStateRuntime;

public class WatcherEngine {

	private WatchedObjectRuntimeContainer container;
	Chronometer chronometer = null;
	
	long chronometerPause = 0;
	long chronometerStart = 0;
	long chronometerStop = 0;
	
	private STATE state = STATE.STOPED; 
	
	enum STATE {
		STOPED,
		PAUSED,
		RUNNING
	}
	
	public WatcherEngine(WatchedObjectRuntimeContainer container, Activity activity, Chronometer chronometer) {
		super();
		this.container = container;
		this.chronometer = chronometer;
	}

	public void reset(){
		
		chronometer.setBase(SystemClock.elapsedRealtime());
		chronometerStart = chronometerStop = SystemClock.elapsedRealtime();
		
		List<WatchedObjectStateRuntime> stateRuntimes = container.getAllWatchedObjectStateRuntimes();
		
		for (WatchedObjectStateRuntime stateRuntime : stateRuntimes) {
			
			stateRuntime.reset();
			
		}
		
	}

	public void start(){
		
		state = STATE.RUNNING;
		chronometerStart = chronometerStop = SystemClock.elapsedRealtime();
		chronometer.start();
		chronometer.setBase(SystemClock.elapsedRealtime());
		
		List<WatchedObjectStateRuntime> stateRuntimes = container.getDefaultWatchedObjectStateRuntimes();
		
		for (WatchedObjectStateRuntime stateRuntime : stateRuntimes) {
			
			stateRuntime.start();
			
		}
		
	}

	public void stop(){
		
		chronometer.stop();
		chronometerStop = SystemClock.elapsedRealtime();
		chronometer.setBase(chronometerStop);
		state = STATE.STOPED;

		List<WatchedObjectStateRuntime> stateRuntimes = container.getAllWatchedObjectStateRuntimes();
		
		for (WatchedObjectStateRuntime stateRuntime : stateRuntimes) {
			
			stateRuntime.stop();
			
		}
		
	}

	public void pause(){
		
		chronometerPause = chronometer.getBase() - SystemClock.elapsedRealtime();
		chronometer.stop();
		state = STATE.PAUSED;

		List<WatchedObjectStateRuntime> stateRuntimes = container.getRunningWatchedObjectStateRuntimes();
		
		for (WatchedObjectStateRuntime stateRuntime : stateRuntimes) {
			
			stateRuntime.pause();
			
		}
		
	}
	
	public void resume(){
		
		chronometer.setBase(chronometerPause + SystemClock.elapsedRealtime());
		chronometer.start();
		state = STATE.RUNNING;

		List<WatchedObjectStateRuntime> stateRuntimes = container.getPausedWatchedObjectStateRuntimes();
		
		for (WatchedObjectStateRuntime stateRuntime : stateRuntimes) {
			
			stateRuntime.resume();
			
		}
		
	}
	
	public void onStateClick( WatchedObjectStateRuntime stateRuntime){
		
		WatchedObjectStateRuntime runningStateRuntime = stateRuntime.getParentWatchedObjectRuntime().getRunningStateRuntime();
		
		System.out.println( "Currently running: " + runningStateRuntime );
		System.out.println( "Pressed state: " + stateRuntime );

		if( stateRuntime.isStopped() ){
			
			if( runningStateRuntime != null ){
				runningStateRuntime.pause();
			}
			
			stateRuntime.start();

		} else if( stateRuntime.isPaused() ){
			
			if( runningStateRuntime != null ){
				runningStateRuntime.pause();
			}

			stateRuntime.resume();
			
		} else if( stateRuntime.isRunning() ){
			
			stateRuntime.pause();
			
		}
		
	}
	
	public void chronometerTickListener() {
		
		List<WatchedObjectStateRuntime> stateRuntimes = container.getRunningWatchedObjectStateRuntimes();
		
		for (WatchedObjectStateRuntime stateRuntime : stateRuntimes) {
			
			stateRuntime.tick();
			
		}
		
	}
	
	public void end(){
		
	}

	public boolean isStopped(){ return STATE.STOPED.equals(this.state); }
	public boolean isPaused() { return STATE.PAUSED.equals(this.state);	}
	public boolean isRunning(){ return STATE.RUNNING.equals(this.state);}
	
}
